<template>
  <div class="app-container">
    <el-form :inline="true" @submit.native.prevent>
      <el-form-item :label="$t('BookStore2[\'Display:WorkTime\']')">
        <el-col :span="11">
          <el-date-picker v-model="listContext.workTimeBegin" type="date" value-format="yyyy-MM-dd" style="width: 100%;" />
        </el-col>
        <el-col :span="11">
          <el-date-picker v-model="listContext.workTimeEnd" type="date" value-format="yyyy-MM-dd" style="width: 100%;" />
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="fetchData">
          <i class="el-icon-search" />
        </el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="$t('BookStore2[\'LoadingWithThreeDot\']')"
      :empty-text="$t('BookStore2[\'NoDataAvailableInDatatable\']')"
      border
      fit
      highlight-current-row
      height="calc(100vh - 225px)"
      @sort-change="sortChange"
    >
      <el-table-column :label="$t('BookStore2[\'Display:Title\']')" prop="title" sortable="custom">
        <template slot-scope="scope">
          <el-button type="text" @click="openTrafficDispatchTaskItem(scope.row)">{{ scope.row.title }}</el-button>
        </template>
      </el-table-column>
      <el-table-column :label="$t('BookStore2[\'Display:Description\']')" prop="description" sortable="custom">
        <template slot-scope="scope">
          {{ scope.row.description }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('BookStore2[\'Display:Type\']')" prop="workType" sortable="custom">
        <template slot-scope="scope">
          {{ $t('BookStore2[\'' + scope.row.workType + '\']') }}
        </template>
      </el-table-column>
<!--      <el-table-column :label="$t('BookStore2[\'Display:WorkTime\']')" prop="workTime" sortable="custom">-->
<!--        <template slot-scope="scope">-->
<!--          {{ scope.row.workTime | dateYMDFormat }}-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column :label="$t('BookStore2[\'Display:Remark\']')" prop="remark" sortable="custom">
        <template slot-scope="scope">
          {{ scope.row.remark }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('BookStore2[\'Status\']')" prop="currentStatus" sortable="custom">
        <template slot-scope="scope">
          {{ $t('BookStore2[\'Enum:TrafficDispatchTaskStatus:' + scope.row.currentStatus + '\']') }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('BookStore2[\'Actions\']')">
        <template slot-scope="scope">
          <el-dropdown trigger="click" @command="clickActionDropDown">
            <el-button type="primary">
              <i class="el-icon-setting" />
              {{ $t('BookStore2[\'Actions\']') }}
              <i class="el-icon-arrow-down" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item :command="{ action: 'edit', row: scope.row }">{{ $t('BookStore2[\'Edit\']') }}</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <div class="block" style="margin-top: 10px">
      <el-pagination
        :page-sizes="pageContext.pageSizes"
        :page-size.sync="listContext.maxResultCount"
        :current-page.sync="pageContext.currentPage"
        :total="pageContext.totalCount"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="fetchData"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { getList } from '@/api/trafficDispatch/trafficDispatchTask'

export default {
  name: 'TrafficDispatchTask',
  data() {
    return {
      list: null,
      listLoading: false,
      pageContext: {
        pageSizes: [10, 25, 50, 100],
        totalCount: 0,
        currentPage: 1
      },
      listContext: {
        workTimeBegin: '',
        workTimeEnd: '',
        sorting: '',
        skipCount: 0,
        maxResultCount: 10
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      if (!this.workTimeBegin) {
        this.workTimeBegin = ''
      }
      if (!this.workTimeEnd) {
        this.workTimeEnd = ''
      }
      getList(this.listContext).then((response) => {
        this.list = response.items
        this.pageContext.totalCount = response.totalCount
        this.listLoading = false
      })
    },
    handleCurrentChange(currentPage) {
      this.listContext.skipCount = (currentPage - 1) * this.listContext.maxResultCount
      this.fetchData()
    },
    sortChange({ prop, order }) {
      this.listContext.sorting = ''
      if (order) {
        this.listContext.sorting = prop + ' ' + order
      }
      this.fetchData()
    },
    clickActionDropDown(ctx) {
      switch (ctx.action) {
        case 'edit':
          // this.openEditDialog(ctx.row.id)
          break
        case 'delete':
          // this.$confirm(this.$t('BookStore2[\'AreYouSure\']')).then(async _ => {
          //   await deleteById(ctx.row.id)
          //   this.$message({
          //     message: this.$t('BookStore2[\'Ok\']'),
          //     type: 'success'
          //   })
          //   this.fetchData()
          // }).catch(_ => {})
          break
      }
    },
    openTrafficDispatchTaskItem(row) {
      this.$router.push({ name: 'TrafficDispatchTaskItem', params: { trafficDispatchTaskId: row.id }})
    }
  }
}
</script>

<style lang="scss" scoped>
</style>

